<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="None">
        
        
        <link rel="shortcut icon" href="img/favicon.ico">
        <title>Web3.js 官方文档中文版(翻译)</title>
        <link href="css/bootstrap-custom.min.css" rel="stylesheet">
        <link href="css/font-awesome.min.css" rel="stylesheet">
        <link href="css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

        <script src="js/jquery-1.10.2.min.js" defer></script>
        <script src="js/bootstrap-3.0.3.min.js" defer></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body class="homepage">

        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">

                <!-- Collapsed navigation -->
                <div class="navbar-header">
                    <!-- Expander button -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href=".">Web3.js 官方文档中文版(翻译)</a>
                </div>

                <!-- Expanded navigation -->
                <div class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li class="active">
                                <a href=".">首页</a>
                            </li>
                            <li >
                                <a href="https://web3.learnblockchain.cn/0.2x.x/">0.20版</a>
                            </li>
                            <li >
                                <a href="https://web3.learnblockchain.cn/1.0/">1.0版</a>
                            </li>
                            <li >
                                <a href="https://learnblockchain.cn">深入浅出区块链博客</a>
                            </li>
                            <li >
                                <a href="https://wiki.learnblockchain.cn/images/dapp.png">DAPP视频课程</a>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <a href="#" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> Search
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
                <div class="col-md-3"><div class="bs-sidebar hidden-print affix well" role="complementary">
    <ul class="nav bs-sidenav">
        <li class="main active"><a href="#web3js">以太坊 web3.js 中文版文档</a></li>
            <li><a href="#020x">0.20.x 中文版翻译</a></li>
            <li><a href="#10">1.0 中文版翻译</a></li>
            <li><a href="#web3">web3介绍</a></li>
            <li><a href="#web3js_1">web3.js版本说明</a></li>
    </ul>
</div></div>
                <div class="col-md-9" role="main">

<h1 id="web3js">以太坊 web3.js 中文版文档</h1>
<h2 id="020x">0.20.x 中文版翻译</h2>
<p><a href="https://web3.learnblockchain.cn/0.2x.x/">web3.js 0.20.x 版本翻译</a></p>
<p>下面是两个0.20.x 版本API的实用DEMO：</p>
<ul>
<li><a href="https://web3.learnblockchain.cn/transDemo.html">使用web3.js API在页面中转账</a></li>
<li><a href="https://web3.learnblockchain.cn/web3_v0.20_demo.html">web3.js 0.20 API 使用Demo</a></li>
<li><a href="https://web3.learnblockchain.cn/ContractEventDemo.html">使用web3.js 与合约交互及监听合约事件</a></li>
</ul>
<h2 id="10">1.0 中文版翻译</h2>
<p><a href="https://web3.learnblockchain.cn/1.0/">web3.js 1.0 文档中文版 正在翻译中</a></p>
<h2 id="web3">web3介绍</h2>
<p>Web3是一套和以太坊节点进行通信的API，如果我们需要基于以太坊来开发去中心化应用，则web3是必须要了解的内容，例如需要通过Web3来获取节点状态，获取账号信息，调用合约、监听合约事件等等。</p>
<blockquote>
<p>注: 智能合约是运行在节点提供的虚拟机上，因此调用智能合约也需要像节点发送请求。</p>
</blockquote>
<p>Web3其实是对节点暴露出来的JSON-RPC接口进行了封装，它有多个语言版本的实现：</p>
<ul>
<li><a href="https://github.com/ethereum/web3.js">JavaScript Web3.js</a></li>
<li><a href="https://github.com/ethereum/web3.py">Python Web3.py</a></li>
<li><a href="https://github.com/airalab/hs-web3">Haskell hs-web3</a></li>
<li><a href="https://github.com/web3j/web3j">Java web3j</a></li>
<li><a href="https://github.com/mslinn/web3j-scala">Scala web3j-scala</a></li>
<li><a href="https://github.com/f-o-a-m/purescript-web3">Purescript purescript-web3</a></li>
<li><a href="https://github.com/sc0Vu/web3.php">PHP web3.php</a></li>
<li><a href="https://github.com/digitaldonkey/ethereum-php">PHP ethereum-php</a></li>
</ul>
<h2 id="web3js_1">web3.js版本说明</h2>
<p>web3.js 有两个不兼容的版本：0.20.x及1.0beta，1.0对0.20版本做了重构，并且引入了Promise来简化异步编程，避免层层的回调嵌套。</p>
<p>做一个对比，下面使用两个版本来获取当前块号：</p>
<pre><code class="js">// 0.20 版本
web3.eth.getBlockNumber(function callback(err, value) {
     console.log(&quot;BlockNumber:&quot; + value)
});
</code></pre>

<pre><code class="js">// 1.0 版本
web3.eth.getBlockNumber().then(console.log);
</code></pre>

<p>在来看看获取账号余额：</p>
<pre><code class="js">// 0.20 版本
web3.eth.getAccounts(function callback1(error, result){
    web3.eth.getBalance(result[0], function callback2(error, value) {
        console.log(&quot;value&quot; + value);
    });
 })
</code></pre>

<pre><code class="js">// 1.0 版本
web3.eth.getAccounts()
   .then((res) =&gt; web3.eth.getBalance(res[0]))
   .then((value) =&gt; console.log(value) );
</code></pre>

<p>使用1.0版本代码上要比0.2版本简洁一些，有一点需要特别注意，web3的多数接口都是异步调用形式，通常最后一个参数是回调函数。</p>
<p>想要学习去中心化应用开发，这门课程不容错过<a href="https://wiki.learnblockchain.cn/course/dapp.html">区块链全栈-以太坊DAPP开发实战</a></p>
<p>本教程由<strong>登链学院</strong>出品，由<strong><a href="https://learnblockchain.cn">深入浅出区块链</a></strong>发布。
登链学院，让每个程序员都懂区块链。</p></div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = ".",
                shortcuts = {"help": 191, "next": 78, "previous": 80, "search": 83};
        </script>
        <script src="js/base.js" defer></script>
        <script src="search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="Search Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Search</h4>
            </div>
            <div class="modal-body">
                <p>
                    From here you can search these documents. Enter
                    your search terms below.
                </p>
                <form role="form">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="Keyboard Shortcuts Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Keyboard Shortcuts</h4>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>

<!--
MkDocs version : 1.0.2
Build Date UTC : 2018-09-13 09:47:20
-->
